<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/16 0016
 * Time: 下午 2:16
 */

namespace App\Console\Commands;


use App\Models\CurrencyLog;
use App\Models\CurrencyLogHour;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class StatisticsOrderHour extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'statistics:order_hour';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'statistics order hour';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @author: yanggang
     * @date: 2018/5/16 0016
     * @throws \Exception
     * @throws \Throwable
     */
    public function handle()
    {
        //统计上一个小时的交易数据
        //开始时间和结束时间

        $end_date = date('Y-m-d H:00:00');
        $start_date = date('Y-m-d H:00:00',strtotime('-1hour',strtotime($end_date)));
        $Date = new \DateTime($start_date);
        $start_minute = $Date->format('H') * 60; //零点到当前的分钟数
        $end_minute = $start_minute+60;

        $time = strtotime(date('Y-m-d',strtotime($start_date)));
        $map = [
            'time'=>$time,
        ];

        $CurrencyLog = new CurrencyLog();
        $list = $CurrencyLog
            ->select(DB::raw('cid,max(high_price*1) max,min(lower_price*1) min,sum(amount*1) sum'))
            ->where($map)
            ->whereBetween('minute_ident',[$start_minute+1,$end_minute])
            ->groupBy('cid')
            ->get();
        $data = [];
        if (collect($list)->isEmpty()){ //如果数据为空 则退出
            return;
        }
        $minute_ident = (new \DateTime($end_date))->format('H');
        if ($minute_ident == 0){
            $minute_ident = 24;
        }
        foreach ($list as $key=>$item){
            $data[$key]['cid']=$item['cid'];
            $data[$key]['lower_price']=$item['min'];
            $data[$key]['high_price']=$item['max'];
            $data[$key]['time']=$time;
            $data[$key]['amount']=$item['sum'];
            $data[$key]['minute_ident']=$minute_ident;
            $data[$key]['open_price'] =$CurrencyLog
                ->where($map)
                ->where(['cid'=>$item['cid']])
                ->whereBetween('minute_ident',[$start_minute+1,$end_minute])
                ->value('open_price');
            $data[$key]['close_price'] =$CurrencyLog
                ->where($map)
                ->where(['cid'=>$item['cid']])
                ->whereBetween('minute_ident',[$start_minute+1,$end_minute])
                ->orderBy('id','DESC')
                ->value('close_price');
        }

        (new CurrencyLogHour())->getConnection()
            ->transaction(function () use ($data) {
                foreach ($data as $key => $item) {
                    (new CurrencyLogHour())->firstOrCreate($item);
                }
            });

    }
}